
#include <acllib.h>

#include <stdio.h>

//#define ORIGINAL

#ifndef ORIGINAL

#include "gf.h"

char filename[MAX_ARR_SIZE];


#endif // ORIGINAL


int Setup()
{

#ifndef ORIGINAL
	i = 0, size = sizeof(rgb) / sizeof(rgb[0]);
#endif
	initWindow("Test", DEFAULT, DEFAULT, WIDTH, HEIGHT);
	initConsole();
#ifdef  ORIGINAL

	printf("Hello\n");
#endif
	beginPaint();
#ifdef  ORIGINAL
	line(10, 10, 100, 100);
#else
	registerMouseEvent(mouseEvent);
	registerKeyboardEvent(KeyDown);

	setBrushColor(GF_RGB(rgb));
	rectangle(0, 0, WIDTH, HEIGHT);

#endif //  ORIGINAL
	endPaint();
	return 0;
}
char paintBuf[MAX_ARR_SIZE];
void mouseEvent(int x, int y, int button, int event)
{
	const int mode[] = { 1,0x10 };//1,0x10
	static int modeIndex = 0;//0,1
	//show the event in the console anyway
	if (event == MOUSEMOVE)return;
	printf("x:%d,y:%d,button:%d,event:%d\n", x, y, button, event);

	if (event == BUTTON_UP)return;

	beginPaint();
	switch (button)
	{
	case RIGHT_BUTTON:
		if (enteringFileName == not_started) {
			modeIndex = (modeIndex + 1) %
				(sizeof(mode) / sizeof(mode[0]));
			break;
		}
		enteringFileName = not_started;
		memset(filename, 0, sizeof(filename));

	case LEFT_BUTTON:
		switch (enteringFileName) {
		case not_started:
			enteringFileName = entering;
			endPaint();
			KeyDown(0, KEY_DOWN);
			return;
		case entering:
			break;
		default:
			break;
		}
		break;
	case MIDDLE_BUTTON:
		switch (event)
		{
		case ROLL_UP:
			rgb[i] = (rgb[i] + mode[modeIndex]) % 0x100;
			break;
		case ROLL_DOWN:
			rgb[i] = (rgb[i] + 0x100 - mode[modeIndex]) % 0x100;
			break;
		case BUTTON_DOWN:
			i++;
			if (i >= size)
				i = 0;
			break;
		default:
			break;
		}

		break;

	}
	setBrushColor(WHITE);
	rectangle(0, 0, 
	WIDTH, HEIGHT);
	setBrushColor(GF_RGB(rgb));
	rectangle(0, 0, 
	WIDTH, HEIGHT - 0x10);
	setTextColor(BLACK);
	setTextBkColor(WHITE);
	sprintf_s(
		paintBuf, sizeof(paintBuf),
		"RGB:%c0x%x,%c0x%x,%c0x%x,mode %s",
		(i == 0) ? '>' : ' ', rgb[0],
		(i == 1) ? '>' : ' ', rgb[1],
		(i == 2) ? '>' : ' ', rgb[2],
		(!modeIndex)
		? "increase by 1"
		: "increase by 0x10(16)"
	);
	paintText(0, 0, paintBuf);
	endPaint();
}
void KeyDown(int key, int event)
{
	const char* saveFileSuffix = ".color.txt";
	static char savebuf[MAX_ARR_SIZE];
	static unsigned int i;
	static FILE* fp;
	//show the event in the console anyway
	printf("key:%d,event:%d\n", key, event);
	if (enteringFileName != entering)return;
	if (event != KEY_DOWN)return;

	beginPaint();

	switch (key)
	{
	case 0:
		break;
	case BACKSPACE:
		if (i > 0)
			savebuf[--i] = '\0';
		break;
	case VK_ESCAPE:
		enteringFileName = not_started;
		memset(filename, 0, sizeof(filename));
		break;
	case ENTER_KEY:
		enteringFileName = finished;
		sprintf_s(
			filename, MAX_ARR_SIZE,
			"%s%s", savebuf, saveFileSuffix
		);
		fopen_s(&fp, filename, "r");
		if (fp) {
			free(fp);
			fp = NULL;
			sprintf_s(
				paintBuf, sizeof(paintBuf),
				"File \'%s\' already exists.",
				filename
			);
			paintText(0, HEIGHT - 0x10, paintBuf);
			endPaint();
			return;
		}
		else {
			fopen_s(&fp, filename, "w");
			if (fp == NULL) {
				sprintf_s(
					paint_buf,
					sizeof(paint_buf),
					"Failed to create file \'%s\'.",
					filename
				);
				paintText(0, HEIGHT - 0x10, paint_buf);
				endPaint();
				memset(filename, 0, sizeof(filename));
				return;
			}
			fprintf(fp, "0x%x 0x%x 0x%x",
			rgb[0], rgb[1], rgb[2]);
			fclose(fp);
			fp = NULL;
			exit(0);
		}
		break;
	default:
		if (i >= MAX_ARR_SIZE - strlen(saveFileSuffix))
			break;
		savebuf[i++] = key;
	}

	setTextColor(BLACK);
	setTextBkColor(WHITE);
	sprintf_s(
		paintBuf, sizeof(paintBuf),
		"Enter the file name:\' %s\'%s",
		savebuf, saveFileSuffix
	);
	paintText(0, HEIGHT - 0x10, paint_buf);


	endPaint();

}